<?php

// Prevent much happening if this script is called directly
if ( !defined('INCS') ) exit;

/*

    utils - Include file.
    -----------------------------------
    author: jah
    date:   2009-04-21T20:28:00 +01:00

    A collection of useful functions which were put here mainly to keep other
    files a little tidier :)

*/


// Sets 'token' session variable if it doesn't exist.
// @return string 40 char sha1 hash
function gen_token()
{
    if ( !isset($_SESSION['token']) ) $_SESSION['token'] = sha1(uniqid(rand(), TRUE));
}


// Adds some details from $_SERVER plus $_GET and $POST arrays to the supplied
// log message.
// @param string message to log.
// @return string message with added information.
function get_log_entry( $msg = '' )
{
    $s = '  ';
    $s .= "Message: $msg";
    $s .= "\n";
    $s .= 'POST: ';
    $s .= var_export( $_POST, 1 );
    $s .= "\n";
    $s .= 'SERVER: ';
    $s .= var_export( get_server_info(), 1 );
    $s .= "\n";
    $s .= 'GET: ';
    $s .= var_export( $_GET, 1 );
    return $s;
}


// Make some simple tests to make sure the form app is configured correctly.
// These tests will eventually halt and redirect to the nice error
function check_config()
{
    // basic checks for the config entry for form element set-up
    global $controls_config;
    if ( !is_array($controls_config) || empty($controls_config) )
    {
        trigger_error( get_log_entry('Config Error: controls_config is not an array or is empty.  Please check config.inc.php.'), E_USER_WARNING);
        display_nice_error();
    }

    foreach( $controls_config as $eKey => $eValue )
    {
        if ( !is_string($eKey) )
        {
            trigger_error( get_log_entry('Config Error: One or more of the controls in controls_config have an invalid name.  Please check config.inc.php.'), E_USER_WARNING);
            display_nice_error();
        }
        if ( !array_key_exists('tagname', $eValue) )
        {
            trigger_error( get_log_entry('Config Error: One or more of the controls in controls_config is missing a tag name.  Please check config.inc.php.'), E_USER_WARNING);
            display_nice_error();
        }
    }


    // check for the submission trigget (e.g. submit button)
    global $submission_trigger;
    if ( !is_string($submission_trigger) || $submission_trigger == '' )
    {
        trigger_error( get_log_entry('Config Error: submission_trigger is invalid.  Please check config.inc.php.'), E_USER_WARNING);
        display_nice_error();
    }
}


// Returns an array of selected keys from $_SERVER and used for logging.
function get_server_info()
{
    return array (
      'SERVER_PROTOCOL' => (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'empty',
      'SERVER_NAME' => (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'empty',
      'SERVER_ADDR' => (isset($_SERVER['SERVER_ADDR'])) ? $_SERVER['SERVER_ADDR'] : 'empty',
      'SERVER_PORT' => (isset($_SERVER['SERVER_PORT'])) ? $_SERVER['SERVER_PORT'] : 'empty',
      'REMOTE_ADDR' => (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : 'empty',
      'REMOTE_PORT' => (isset($_SERVER['REMOTE_PORT'])) ? $_SERVER['REMOTE_PORT'] : 'empty',
      'REQUEST_TIME' => (isset($_SERVER['REQUEST_TIME'])) ? date( "c", $_SERVER['REQUEST_TIME']) : 'empty',
      'REQUEST_METHOD' => (isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : 'empty',
      'REQUEST_URI' => (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : 'empty',
      'QUERY_STRING' => (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : 'empty',
      'HTTP_HOST' => (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : 'empty',
      'HTTP_REFERER' => (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : 'empty',
      'HTTP_USER_AGENT' => (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : 'empty',
      'HTTP_COOKIE' => (isset($_SERVER['HTTP_COOKIE'])) ? $_SERVER['HTTP_COOKIE'] : 'empty',
      'CONTENT_TYPE' => (isset($_SERVER['CONTENT_TYPE'])) ? $_SERVER['CONTENT_TYPE'] : 'empty',
      'CONTENT_LENGTH' => (isset($_SERVER['CONTENT_LENGTH'])) ? $_SERVER['CONTENT_LENGTH'] : 'empty',
      'HTTP_ACCEPT' => (isset($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : 'empty',
      'HTTP_ACCEPT_ENCODING' => (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : 'empty',
      'HTTP_ACCEPT_CHARSET' => (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) ? $_SERVER['HTTP_ACCEPT_CHARSET'] : 'empty',
      'HTTP_ACCEPT_LANGUAGE' => (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : 'empty',
      'HTTP_KEEP_ALIVE' => (isset($_SERVER['HTTP_KEEP_ALIVE'])) ? $_SERVER['HTTP_KEEP_ALIVE'] : 'empty',
      'HTTP_CONNECTION' => (isset($_SERVER['HTTP_CONNECTION'])) ? $_SERVER['HTTP_CONNECTION'] : 'empty',
      'SCRIPT_FILENAME' => (isset($_SERVER['SCRIPT_FILENAME'])) ? $_SERVER['SCRIPT_FILENAME'] : 'empty',
      'SCRIPT_NAME' => (isset($_SERVER['SCRIPT_NAME'])) ? $_SERVER['SCRIPT_NAME'] : 'empty',
      'PHP_SELF' => (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : 'empty'
    );
}
